\hypertarget{group__thread}{}\section{线程}
\label{group__thread}\index{线程@{线程}}
\subsection*{函数}
\begin{DoxyCompactItemize}
\item 
\hyperlink{struct__thread__runner__t}{thread\+\_\+runner\+\_\+t} $\ast$ \hyperlink{group__thread_gac20d7726e152e0b49209ce228893c25d}{thread\+\_\+runner\+\_\+create} (thread\+\_\+func\+\_\+t func, void $\ast$params)
\item 
void \hyperlink{group__thread_ga8b9d3d45b6055971fc40f24ec6572cf6}{thread\+\_\+runner\+\_\+destroy} (\hyperlink{struct__thread__runner__t}{thread\+\_\+runner\+\_\+t} $\ast$runner)
\item 
int \hyperlink{group__thread_ga1114fd962e310f9d31ed4e83d8a21897}{thread\+\_\+runner\+\_\+start} (\hyperlink{struct__thread__runner__t}{thread\+\_\+runner\+\_\+t} $\ast$runner, int stack\+\_\+size)
\item 
void \hyperlink{group__thread_gaa035f190ffd3d7f117b8fbb58716f042}{thread\+\_\+runner\+\_\+stop} (\hyperlink{struct__thread__runner__t}{thread\+\_\+runner\+\_\+t} $\ast$runner)
\item 
int \hyperlink{group__thread_ga93f88ba77e7eab786da6fe7d1f681a2e}{thread\+\_\+runner\+\_\+start\+\_\+loop} (\hyperlink{struct__thread__runner__t}{thread\+\_\+runner\+\_\+t} $\ast$runner, \hyperlink{struct__loop__t}{loop\+\_\+t} $\ast$loop, int stack\+\_\+size)
\item 
int \hyperlink{group__thread_ga9191991e2bd7f91fe68324c2855d581d}{thread\+\_\+runner\+\_\+start\+\_\+timer\+\_\+loop} (\hyperlink{struct__thread__runner__t}{thread\+\_\+runner\+\_\+t} $\ast$runner, \hyperlink{struct__ktimer__loop__t}{ktimer\+\_\+loop\+\_\+t} $\ast$timer\+\_\+loop, int stack\+\_\+size)
\item 
void \hyperlink{group__thread_ga813a718c56e0923be9396630c8460794}{thread\+\_\+runner\+\_\+join} (\hyperlink{struct__thread__runner__t}{thread\+\_\+runner\+\_\+t} $\ast$runner)
\item 
int \hyperlink{group__thread_ga95c00ec3d6ef1710cc8d8bb8cb7e585d}{thread\+\_\+runner\+\_\+check\+\_\+start} (\hyperlink{struct__thread__runner__t}{thread\+\_\+runner\+\_\+t} $\ast$runner)
\item 
void $\ast$ \hyperlink{group__thread_gaf67fa2b2cfefc2de422a31b9dfad2bca}{thread\+\_\+runner\+\_\+get\+\_\+params} (\hyperlink{struct__thread__runner__t}{thread\+\_\+runner\+\_\+t} $\ast$runner)
\item 
thread\+\_\+id\+\_\+t \hyperlink{group__thread_gaecf10907eee00ec0e807271c906a6645}{thread\+\_\+get\+\_\+self\+\_\+id} ()
\item 
void \hyperlink{group__thread_gabddf3ac30aac3f22f8c53b7517e6eb35}{thread\+\_\+sleep\+\_\+ms} (int ms)
\item 
int \hyperlink{group__thread_gaa187b2cd2c906680890fb4ea2833d665}{thread\+\_\+set\+\_\+tls\+\_\+data} (\hyperlink{struct__thread__runner__t}{thread\+\_\+runner\+\_\+t} $\ast$runner, void $\ast$data)
\item 
void $\ast$ \hyperlink{group__thread_ga63c9be5b8fb04c0b26382f51b7bdf237}{thread\+\_\+get\+\_\+tls\+\_\+data} (\hyperlink{struct__thread__runner__t}{thread\+\_\+runner\+\_\+t} $\ast$runner)
\item 
atomic\+\_\+counter\+\_\+t \hyperlink{group__thread_gade4f41f1cd519f2719b5114ce7801232}{atomic\+\_\+counter\+\_\+inc} (atomic\+\_\+counter\+\_\+t $\ast$counter)
\item 
atomic\+\_\+counter\+\_\+t \hyperlink{group__thread_gafc2df03d75cf9168b72e8c5e11953437}{atomic\+\_\+counter\+\_\+dec} (atomic\+\_\+counter\+\_\+t $\ast$counter)
\item 
int \hyperlink{group__thread_ga141897cac9743394a99ff7780daa6a80}{atomic\+\_\+counter\+\_\+zero} (atomic\+\_\+counter\+\_\+t $\ast$counter)
\end{DoxyCompactItemize}


\subsection{详细描述}

\begin{DoxyPre}
线程\end{DoxyPre}



\begin{DoxyPre}线程API提供了基本的线程相关操作:\end{DoxyPre}



\begin{DoxyPre}1. 线程建立销毁
2. TLS
3. 原子操作\end{DoxyPre}



\begin{DoxyPre}thread\_runner\_start\_loop可以直接以loop\_t作为参数在线程内运行loop\_run
thread\_runner\_start\_timer\_loop可以直接以ktimer\_loop\_t作为参数在线程内运行ktimer\_loop\_run\end{DoxyPre}



\begin{DoxyPre}\end{DoxyPre}
 

\subsection{函数说明}
\hypertarget{group__thread_gafc2df03d75cf9168b72e8c5e11953437}{}\index{线程@{线程}!atomic\+\_\+counter\+\_\+dec@{atomic\+\_\+counter\+\_\+dec}}
\index{atomic\+\_\+counter\+\_\+dec@{atomic\+\_\+counter\+\_\+dec}!线程@{线程}}
\subsubsection[{atomic\+\_\+counter\+\_\+dec}]{\setlength{\rightskip}{0pt plus 5cm}atomic\+\_\+counter\+\_\+t atomic\+\_\+counter\+\_\+dec (
\begin{DoxyParamCaption}
\item[{atomic\+\_\+counter\+\_\+t $\ast$}]{counter}
\end{DoxyParamCaption}
)}\label{group__thread_gafc2df03d75cf9168b72e8c5e11953437}
原子操作 -\/ 递减 
\begin{DoxyParams}{参数}
{\em counter} & atomic\+\_\+counter\+\_\+t实例 \\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{返回}
递减后的值 
\end{DoxyReturn}
\hypertarget{group__thread_gade4f41f1cd519f2719b5114ce7801232}{}\index{线程@{线程}!atomic\+\_\+counter\+\_\+inc@{atomic\+\_\+counter\+\_\+inc}}
\index{atomic\+\_\+counter\+\_\+inc@{atomic\+\_\+counter\+\_\+inc}!线程@{线程}}
\subsubsection[{atomic\+\_\+counter\+\_\+inc}]{\setlength{\rightskip}{0pt plus 5cm}atomic\+\_\+counter\+\_\+t atomic\+\_\+counter\+\_\+inc (
\begin{DoxyParamCaption}
\item[{atomic\+\_\+counter\+\_\+t $\ast$}]{counter}
\end{DoxyParamCaption}
)}\label{group__thread_gade4f41f1cd519f2719b5114ce7801232}
原子操作 -\/ 递增 
\begin{DoxyParams}{参数}
{\em counter} & atomic\+\_\+counter\+\_\+t实例 \\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{返回}
递增后的值 
\end{DoxyReturn}
\hypertarget{group__thread_ga141897cac9743394a99ff7780daa6a80}{}\index{线程@{线程}!atomic\+\_\+counter\+\_\+zero@{atomic\+\_\+counter\+\_\+zero}}
\index{atomic\+\_\+counter\+\_\+zero@{atomic\+\_\+counter\+\_\+zero}!线程@{线程}}
\subsubsection[{atomic\+\_\+counter\+\_\+zero}]{\setlength{\rightskip}{0pt plus 5cm}int atomic\+\_\+counter\+\_\+zero (
\begin{DoxyParamCaption}
\item[{atomic\+\_\+counter\+\_\+t $\ast$}]{counter}
\end{DoxyParamCaption}
)}\label{group__thread_ga141897cac9743394a99ff7780daa6a80}
原子操作 -\/ 是否为零 
\begin{DoxyParams}{参数}
{\em counter} & atomic\+\_\+counter\+\_\+t实例 \\
\hline
\end{DoxyParams}

\begin{DoxyRetVals}{返回值}
{\em 0} & 非零 \\
\hline
{\em 其他} & 零 \\
\hline
\end{DoxyRetVals}
\hypertarget{group__thread_gaecf10907eee00ec0e807271c906a6645}{}\index{线程@{线程}!thread\+\_\+get\+\_\+self\+\_\+id@{thread\+\_\+get\+\_\+self\+\_\+id}}
\index{thread\+\_\+get\+\_\+self\+\_\+id@{thread\+\_\+get\+\_\+self\+\_\+id}!线程@{线程}}
\subsubsection[{thread\+\_\+get\+\_\+self\+\_\+id}]{\setlength{\rightskip}{0pt plus 5cm}thread\+\_\+id\+\_\+t thread\+\_\+get\+\_\+self\+\_\+id (
\begin{DoxyParamCaption}
{}
\end{DoxyParamCaption}
)}\label{group__thread_gaecf10907eee00ec0e807271c906a6645}
取得线程\+I\+D \begin{DoxyReturn}{返回}
线程\+I\+D 
\end{DoxyReturn}
\hypertarget{group__thread_ga63c9be5b8fb04c0b26382f51b7bdf237}{}\index{线程@{线程}!thread\+\_\+get\+\_\+tls\+\_\+data@{thread\+\_\+get\+\_\+tls\+\_\+data}}
\index{thread\+\_\+get\+\_\+tls\+\_\+data@{thread\+\_\+get\+\_\+tls\+\_\+data}!线程@{线程}}
\subsubsection[{thread\+\_\+get\+\_\+tls\+\_\+data}]{\setlength{\rightskip}{0pt plus 5cm}void$\ast$ thread\+\_\+get\+\_\+tls\+\_\+data (
\begin{DoxyParamCaption}
\item[{{\bf thread\+\_\+runner\+\_\+t} $\ast$}]{runner}
\end{DoxyParamCaption}
)}\label{group__thread_ga63c9be5b8fb04c0b26382f51b7bdf237}
取得线程本地存储 
\begin{DoxyParams}{参数}
{\em runner} & thread\+\_\+runner\+\_\+t实例 \\
\hline
\end{DoxyParams}

\begin{DoxyRetVals}{返回值}
{\em 0} & 获取失败或不存在 \\
\hline
{\em 有效指针} & \\
\hline
\end{DoxyRetVals}
\hypertarget{group__thread_ga95c00ec3d6ef1710cc8d8bb8cb7e585d}{}\index{线程@{线程}!thread\+\_\+runner\+\_\+check\+\_\+start@{thread\+\_\+runner\+\_\+check\+\_\+start}}
\index{thread\+\_\+runner\+\_\+check\+\_\+start@{thread\+\_\+runner\+\_\+check\+\_\+start}!线程@{线程}}
\subsubsection[{thread\+\_\+runner\+\_\+check\+\_\+start}]{\setlength{\rightskip}{0pt plus 5cm}int thread\+\_\+runner\+\_\+check\+\_\+start (
\begin{DoxyParamCaption}
\item[{{\bf thread\+\_\+runner\+\_\+t} $\ast$}]{runner}
\end{DoxyParamCaption}
)}\label{group__thread_ga95c00ec3d6ef1710cc8d8bb8cb7e585d}
检查线程是否正在运行 
\begin{DoxyParams}{参数}
{\em runner} & thread\+\_\+runner\+\_\+t实例 \\
\hline
\end{DoxyParams}

\begin{DoxyRetVals}{返回值}
{\em 0} & 未运行 \\
\hline
{\em 非零} & 正在运行 \\
\hline
\end{DoxyRetVals}
\hypertarget{group__thread_gac20d7726e152e0b49209ce228893c25d}{}\index{线程@{线程}!thread\+\_\+runner\+\_\+create@{thread\+\_\+runner\+\_\+create}}
\index{thread\+\_\+runner\+\_\+create@{thread\+\_\+runner\+\_\+create}!线程@{线程}}
\subsubsection[{thread\+\_\+runner\+\_\+create}]{\setlength{\rightskip}{0pt plus 5cm}{\bf thread\+\_\+runner\+\_\+t}$\ast$ thread\+\_\+runner\+\_\+create (
\begin{DoxyParamCaption}
\item[{thread\+\_\+func\+\_\+t}]{func, }
\item[{void $\ast$}]{params}
\end{DoxyParamCaption}
)}\label{group__thread_gac20d7726e152e0b49209ce228893c25d}
创建一个线程 
\begin{DoxyParams}{参数}
{\em func} & 线程函数 \\
\hline
{\em params} & 参数 \\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{返回}
thread\+\_\+runner\+\_\+t实例 
\end{DoxyReturn}
\hypertarget{group__thread_ga8b9d3d45b6055971fc40f24ec6572cf6}{}\index{线程@{线程}!thread\+\_\+runner\+\_\+destroy@{thread\+\_\+runner\+\_\+destroy}}
\index{thread\+\_\+runner\+\_\+destroy@{thread\+\_\+runner\+\_\+destroy}!线程@{线程}}
\subsubsection[{thread\+\_\+runner\+\_\+destroy}]{\setlength{\rightskip}{0pt plus 5cm}void thread\+\_\+runner\+\_\+destroy (
\begin{DoxyParamCaption}
\item[{{\bf thread\+\_\+runner\+\_\+t} $\ast$}]{runner}
\end{DoxyParamCaption}
)}\label{group__thread_ga8b9d3d45b6055971fc40f24ec6572cf6}
销毁一个线程 
\begin{DoxyParams}{参数}
{\em runner} & thread\+\_\+runner\+\_\+t实例 \\
\hline
\end{DoxyParams}
\hypertarget{group__thread_gaf67fa2b2cfefc2de422a31b9dfad2bca}{}\index{线程@{线程}!thread\+\_\+runner\+\_\+get\+\_\+params@{thread\+\_\+runner\+\_\+get\+\_\+params}}
\index{thread\+\_\+runner\+\_\+get\+\_\+params@{thread\+\_\+runner\+\_\+get\+\_\+params}!线程@{线程}}
\subsubsection[{thread\+\_\+runner\+\_\+get\+\_\+params}]{\setlength{\rightskip}{0pt plus 5cm}void$\ast$ thread\+\_\+runner\+\_\+get\+\_\+params (
\begin{DoxyParamCaption}
\item[{{\bf thread\+\_\+runner\+\_\+t} $\ast$}]{runner}
\end{DoxyParamCaption}
)}\label{group__thread_gaf67fa2b2cfefc2de422a31b9dfad2bca}
取得线程运行参数，thread\+\_\+runner\+\_\+create()第二个参数 
\begin{DoxyParams}{参数}
{\em runner} & thread\+\_\+runner\+\_\+t实例 \\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{返回}
线程运行参数 
\end{DoxyReturn}
\hypertarget{group__thread_ga813a718c56e0923be9396630c8460794}{}\index{线程@{线程}!thread\+\_\+runner\+\_\+join@{thread\+\_\+runner\+\_\+join}}
\index{thread\+\_\+runner\+\_\+join@{thread\+\_\+runner\+\_\+join}!线程@{线程}}
\subsubsection[{thread\+\_\+runner\+\_\+join}]{\setlength{\rightskip}{0pt plus 5cm}void thread\+\_\+runner\+\_\+join (
\begin{DoxyParamCaption}
\item[{{\bf thread\+\_\+runner\+\_\+t} $\ast$}]{runner}
\end{DoxyParamCaption}
)}\label{group__thread_ga813a718c56e0923be9396630c8460794}
等待线程终止 
\begin{DoxyParams}{参数}
{\em runner} & thread\+\_\+runner\+\_\+t实例 \\
\hline
\end{DoxyParams}
\hypertarget{group__thread_ga1114fd962e310f9d31ed4e83d8a21897}{}\index{线程@{线程}!thread\+\_\+runner\+\_\+start@{thread\+\_\+runner\+\_\+start}}
\index{thread\+\_\+runner\+\_\+start@{thread\+\_\+runner\+\_\+start}!线程@{线程}}
\subsubsection[{thread\+\_\+runner\+\_\+start}]{\setlength{\rightskip}{0pt plus 5cm}int thread\+\_\+runner\+\_\+start (
\begin{DoxyParamCaption}
\item[{{\bf thread\+\_\+runner\+\_\+t} $\ast$}]{runner, }
\item[{int}]{stack\+\_\+size}
\end{DoxyParamCaption}
)}\label{group__thread_ga1114fd962e310f9d31ed4e83d8a21897}
启动线程 
\begin{DoxyParams}{参数}
{\em runner} & thread\+\_\+runner\+\_\+t实例 \\
\hline
{\em stack\+\_\+size} & 线程栈大小（字节） \\
\hline
\end{DoxyParams}

\begin{DoxyRetVals}{返回值}
{\em error\+\_\+ok} & 成功 \\
\hline
{\em 其他} & 失败 \\
\hline
\end{DoxyRetVals}
\hypertarget{group__thread_ga93f88ba77e7eab786da6fe7d1f681a2e}{}\index{线程@{线程}!thread\+\_\+runner\+\_\+start\+\_\+loop@{thread\+\_\+runner\+\_\+start\+\_\+loop}}
\index{thread\+\_\+runner\+\_\+start\+\_\+loop@{thread\+\_\+runner\+\_\+start\+\_\+loop}!线程@{线程}}
\subsubsection[{thread\+\_\+runner\+\_\+start\+\_\+loop}]{\setlength{\rightskip}{0pt plus 5cm}int thread\+\_\+runner\+\_\+start\+\_\+loop (
\begin{DoxyParamCaption}
\item[{{\bf thread\+\_\+runner\+\_\+t} $\ast$}]{runner, }
\item[{{\bf loop\+\_\+t} $\ast$}]{loop, }
\item[{int}]{stack\+\_\+size}
\end{DoxyParamCaption}
)}\label{group__thread_ga93f88ba77e7eab786da6fe7d1f681a2e}
在线程内运行loop\+\_\+run() 
\begin{DoxyParams}{参数}
{\em runner} & thread\+\_\+runner\+\_\+t实例 \\
\hline
{\em loop} & loop\+\_\+t实例 \\
\hline
{\em stack\+\_\+size} & 线程栈大小（字节） \\
\hline
\end{DoxyParams}

\begin{DoxyRetVals}{返回值}
{\em error\+\_\+ok} & 成功 \\
\hline
{\em 其他} & 失败 \\
\hline
\end{DoxyRetVals}
\hypertarget{group__thread_ga9191991e2bd7f91fe68324c2855d581d}{}\index{线程@{线程}!thread\+\_\+runner\+\_\+start\+\_\+timer\+\_\+loop@{thread\+\_\+runner\+\_\+start\+\_\+timer\+\_\+loop}}
\index{thread\+\_\+runner\+\_\+start\+\_\+timer\+\_\+loop@{thread\+\_\+runner\+\_\+start\+\_\+timer\+\_\+loop}!线程@{线程}}
\subsubsection[{thread\+\_\+runner\+\_\+start\+\_\+timer\+\_\+loop}]{\setlength{\rightskip}{0pt plus 5cm}int thread\+\_\+runner\+\_\+start\+\_\+timer\+\_\+loop (
\begin{DoxyParamCaption}
\item[{{\bf thread\+\_\+runner\+\_\+t} $\ast$}]{runner, }
\item[{{\bf ktimer\+\_\+loop\+\_\+t} $\ast$}]{timer\+\_\+loop, }
\item[{int}]{stack\+\_\+size}
\end{DoxyParamCaption}
)}\label{group__thread_ga9191991e2bd7f91fe68324c2855d581d}
在线程内运行timer\+\_\+loop\+\_\+run() 
\begin{DoxyParams}{参数}
{\em runner} & thread\+\_\+runner\+\_\+t实例 \\
\hline
{\em timer\+\_\+loop} & ktimer\+\_\+loop\+\_\+t实例 \\
\hline
{\em stack\+\_\+size} & 线程栈大小（字节） \\
\hline
\end{DoxyParams}

\begin{DoxyRetVals}{返回值}
{\em error\+\_\+ok} & 成功 \\
\hline
{\em 其他} & 失败 \\
\hline
\end{DoxyRetVals}
\hypertarget{group__thread_gaa035f190ffd3d7f117b8fbb58716f042}{}\index{线程@{线程}!thread\+\_\+runner\+\_\+stop@{thread\+\_\+runner\+\_\+stop}}
\index{thread\+\_\+runner\+\_\+stop@{thread\+\_\+runner\+\_\+stop}!线程@{线程}}
\subsubsection[{thread\+\_\+runner\+\_\+stop}]{\setlength{\rightskip}{0pt plus 5cm}void thread\+\_\+runner\+\_\+stop (
\begin{DoxyParamCaption}
\item[{{\bf thread\+\_\+runner\+\_\+t} $\ast$}]{runner}
\end{DoxyParamCaption}
)}\label{group__thread_gaa035f190ffd3d7f117b8fbb58716f042}
停止线程 
\begin{DoxyParams}{参数}
{\em runner} & thread\+\_\+runner\+\_\+t实例 \\
\hline
\end{DoxyParams}
\hypertarget{group__thread_gaa187b2cd2c906680890fb4ea2833d665}{}\index{线程@{线程}!thread\+\_\+set\+\_\+tls\+\_\+data@{thread\+\_\+set\+\_\+tls\+\_\+data}}
\index{thread\+\_\+set\+\_\+tls\+\_\+data@{thread\+\_\+set\+\_\+tls\+\_\+data}!线程@{线程}}
\subsubsection[{thread\+\_\+set\+\_\+tls\+\_\+data}]{\setlength{\rightskip}{0pt plus 5cm}int thread\+\_\+set\+\_\+tls\+\_\+data (
\begin{DoxyParamCaption}
\item[{{\bf thread\+\_\+runner\+\_\+t} $\ast$}]{runner, }
\item[{void $\ast$}]{data}
\end{DoxyParamCaption}
)}\label{group__thread_gaa187b2cd2c906680890fb4ea2833d665}
设置线程本地存储 
\begin{DoxyParams}{参数}
{\em runner} & thread\+\_\+runner\+\_\+t实例 \\
\hline
{\em data} & 自定义数据指针 \\
\hline
\end{DoxyParams}

\begin{DoxyRetVals}{返回值}
{\em error\+\_\+ok} & 成功 \\
\hline
{\em 其他} & 失败 \\
\hline
\end{DoxyRetVals}
\hypertarget{group__thread_gabddf3ac30aac3f22f8c53b7517e6eb35}{}\index{线程@{线程}!thread\+\_\+sleep\+\_\+ms@{thread\+\_\+sleep\+\_\+ms}}
\index{thread\+\_\+sleep\+\_\+ms@{thread\+\_\+sleep\+\_\+ms}!线程@{线程}}
\subsubsection[{thread\+\_\+sleep\+\_\+ms}]{\setlength{\rightskip}{0pt plus 5cm}void thread\+\_\+sleep\+\_\+ms (
\begin{DoxyParamCaption}
\item[{int}]{ms}
\end{DoxyParamCaption}
)}\label{group__thread_gabddf3ac30aac3f22f8c53b7517e6eb35}
睡眠 
\begin{DoxyParams}{参数}
{\em ms} & 睡眠时间（毫秒） \\
\hline
\end{DoxyParams}
